﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:inspectionMaterial
	/// </summary>
	public partial class inspectionMaterial
	{
		public inspectionMaterial()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("inspection_id", "inspectionMaterial"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int inspection_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from inspectionMaterial");
			strSql.Append(" where inspection_id=@inspection_id");
			SqlParameter[] parameters = {
					new SqlParameter("@inspection_id", SqlDbType.Int,4)
			};
			parameters[0].Value = inspection_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        public int AddE(lcs.Model.inspectionMaterial model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into inspectionMaterial(");
            strSql.Append("submitUid,out_storage_detail_Id,submitQuestion,submitTime)");
            strSql.Append(" values (");
            strSql.Append("@submitUid,@out_storage_detail_Id,@submitQuestion,@submitTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@submitUid", SqlDbType.Int,4),
                    new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4),
                    new SqlParameter("@submitQuestion", SqlDbType.VarChar,300),
                    new SqlParameter("@submitTime", SqlDbType.DateTime)
                   };
            parameters[0].Value = model.submitUid;
            parameters[1].Value = model.out_storage_detail_Id;
            parameters[2].Value = model.submitQuestion;
            parameters[3].Value = model.submitTime;           

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(lcs.Model.inspectionMaterial model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into inspectionMaterial(");
			strSql.Append("submitUid,out_storage_detail_Id,submitQuestion,submitTime,checkUid,dealUid,dealStatus,dealResult,dealTime,useMoney)");
			strSql.Append(" values (");
			strSql.Append("@submitUid,@out_storage_detail_Id,@submitQuestion,@submitTime,@checkUid,@dealUid,@dealStatus,@dealResult,@dealTime,@useMoney)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@submitUid", SqlDbType.Int,4),
					new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4),
					new SqlParameter("@submitQuestion", SqlDbType.VarChar,300),
					new SqlParameter("@submitTime", SqlDbType.DateTime),
					new SqlParameter("@checkUid", SqlDbType.Int,4),
					new SqlParameter("@dealUid", SqlDbType.Int,4),
					new SqlParameter("@dealStatus", SqlDbType.Int,4),
					new SqlParameter("@dealResult", SqlDbType.VarChar,300),
					new SqlParameter("@dealTime", SqlDbType.DateTime),
					new SqlParameter("@useMoney", SqlDbType.Money,8)};
			parameters[0].Value = model.submitUid;
			parameters[1].Value = model.out_storage_detail_Id;
			parameters[2].Value = model.submitQuestion;
			parameters[3].Value = model.submitTime;
			parameters[4].Value = model.checkUid;
			parameters[5].Value = model.dealUid;
			parameters[6].Value = model.dealStatus;
			parameters[7].Value = model.dealResult;
			parameters[8].Value = model.dealTime;
			parameters[9].Value = model.useMoney;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcs.Model.inspectionMaterial model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update inspectionMaterial set ");
			strSql.Append("submitUid=@submitUid,");
			strSql.Append("out_storage_detail_Id=@out_storage_detail_Id,");
			strSql.Append("submitQuestion=@submitQuestion,");
			strSql.Append("submitTime=@submitTime,");
			strSql.Append("checkUid=@checkUid,");
			strSql.Append("dealUid=@dealUid,");
			strSql.Append("dealStatus=@dealStatus,");
			strSql.Append("dealResult=@dealResult,");
			strSql.Append("dealTime=@dealTime,");
			strSql.Append("useMoney=@useMoney");
			strSql.Append(" where inspection_id=@inspection_id");
			SqlParameter[] parameters = {
					new SqlParameter("@submitUid", SqlDbType.Int,4),
					new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4),
					new SqlParameter("@submitQuestion", SqlDbType.VarChar,300),
					new SqlParameter("@submitTime", SqlDbType.DateTime),
					new SqlParameter("@checkUid", SqlDbType.Int,4),
					new SqlParameter("@dealUid", SqlDbType.Int,4),
					new SqlParameter("@dealStatus", SqlDbType.Int,4),
					new SqlParameter("@dealResult", SqlDbType.VarChar,300),
					new SqlParameter("@dealTime", SqlDbType.DateTime),
					new SqlParameter("@useMoney", SqlDbType.Money,8),
					new SqlParameter("@inspection_id", SqlDbType.Int,4)};
			parameters[0].Value = model.submitUid;
			parameters[1].Value = model.out_storage_detail_Id;
			parameters[2].Value = model.submitQuestion;
			parameters[3].Value = model.submitTime;
			parameters[4].Value = model.checkUid;
			parameters[5].Value = model.dealUid;
			parameters[6].Value = model.dealStatus;
			parameters[7].Value = model.dealResult;
			parameters[8].Value = model.dealTime;
			parameters[9].Value = model.useMoney;
			parameters[10].Value = model.inspection_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int inspection_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from inspectionMaterial ");
			strSql.Append(" where inspection_id=@inspection_id");
			SqlParameter[] parameters = {
					new SqlParameter("@inspection_id", SqlDbType.Int,4)
			};
			parameters[0].Value = inspection_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string inspection_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from inspectionMaterial ");
			strSql.Append(" where inspection_id in ("+inspection_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.inspectionMaterial GetModel(int inspection_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 inspection_id,submitUid,out_storage_detail_Id,submitQuestion,submitTime,checkUid,dealUid,dealStatus,dealResult,dealTime,useMoney from inspectionMaterial ");
			strSql.Append(" where inspection_id=@inspection_id");
			SqlParameter[] parameters = {
					new SqlParameter("@inspection_id", SqlDbType.Int,4)
			};
			parameters[0].Value = inspection_id;

			lcs.Model.inspectionMaterial model=new lcs.Model.inspectionMaterial();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.inspectionMaterial DataRowToModel(DataRow row)
		{
			lcs.Model.inspectionMaterial model=new lcs.Model.inspectionMaterial();
			if (row != null)
			{
				if(row["inspection_id"]!=null && row["inspection_id"].ToString()!="")
				{
					model.inspection_id=int.Parse(row["inspection_id"].ToString());
				}
				if(row["submitUid"]!=null && row["submitUid"].ToString()!="")
				{
					model.submitUid=int.Parse(row["submitUid"].ToString());
				}
				if(row["out_storage_detail_Id"]!=null && row["out_storage_detail_Id"].ToString()!="")
				{
					model.out_storage_detail_Id=int.Parse(row["out_storage_detail_Id"].ToString());
				}
				if(row["submitQuestion"]!=null)
				{
					model.submitQuestion=row["submitQuestion"].ToString();
				}
				if(row["submitTime"]!=null && row["submitTime"].ToString()!="")
				{
					model.submitTime=DateTime.Parse(row["submitTime"].ToString());
				}
				if(row["checkUid"]!=null && row["checkUid"].ToString()!="")
				{
					model.checkUid=int.Parse(row["checkUid"].ToString());
				}
				if(row["dealUid"]!=null && row["dealUid"].ToString()!="")
				{
					model.dealUid=int.Parse(row["dealUid"].ToString());
				}
				if(row["dealStatus"]!=null && row["dealStatus"].ToString()!="")
				{
					model.dealStatus=int.Parse(row["dealStatus"].ToString());
				}
				if(row["dealResult"]!=null)
				{
					model.dealResult=row["dealResult"].ToString();
				}
				if(row["dealTime"]!=null && row["dealTime"].ToString()!="")
				{
					model.dealTime=DateTime.Parse(row["dealTime"].ToString());
				}
				if(row["useMoney"]!=null && row["useMoney"].ToString()!="")
				{
					model.useMoney=decimal.Parse(row["useMoney"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM VinspectionMaterial  where 1=1");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" inspection_id,submitUid,out_storage_detail_Id,submitQuestion,submitTime,checkUid,dealUid,dealStatus,dealResult,dealTime,useMoney ");
			strSql.Append(" FROM inspectionMaterial ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM inspectionMaterial ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.inspection_id desc");
			}
			strSql.Append(")AS Row, T.*  from inspectionMaterial T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "inspectionMaterial";
			parameters[1].Value = "inspection_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        public bool Updateuid(int uid, string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update inspectionMaterial set ");
            strSql.Append("dealUid=@dealUid ");
            strSql.Append(" where inspection_id in(" + ids + ")");
            SqlParameter[] parameters = {
                    new SqlParameter("@dealUid", SqlDbType.Int,4)
                    };
            parameters[0].Value = uid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool UpdateTime(DateTime dt,int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update inspectionMaterial set ");
            strSql.Append("dealTime=@dealTime ");
            strSql.Append(" where inspection_id =" + id);
            SqlParameter[] parameters = {
                    new SqlParameter("@dealTime", SqlDbType.DateTime)
                    };
            parameters[0].Value = dt;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool UpdateJG(lcs.Model.inspectionMaterial model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update inspectionMaterial set ");            
            strSql.Append("dealStatus=@dealStatus,");
            strSql.Append("dealResult=@dealResult,");            
            strSql.Append("useMoney=@useMoney");
            strSql.Append(" where inspection_id=@inspection_id and dealUid=@dealUid");
            SqlParameter[] parameters = {                    
                    new SqlParameter("@dealStatus", SqlDbType.Int,4),
                    new SqlParameter("@dealResult", SqlDbType.VarChar,300),                    
                    new SqlParameter("@useMoney", SqlDbType.Money,8),
                    new SqlParameter("@inspection_id", SqlDbType.Int,4),
            new SqlParameter("@dealUid", SqlDbType.Int,4)};
            parameters[0].Value = model.dealStatus;
            parameters[1].Value = model.dealResult;
            parameters[2].Value =model.useMoney;
            parameters[3].Value = model.inspection_id;
            parameters[4].Value = model.dealUid;            

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

